class Instructor < ActiveRecord::Base
  has_many :teachings
  has_many :events, :through=>:teachings, :dependent=>:destroy
  validates_presence_of :first_name
  validates_presence_of :last_name

  def name
    first_name + " " + last_name
  end

  def Instructor.find_by_lastname(name)
    r = Instructor.find(:all, :conditions => {:last_name => name.upcase})
    return r.empty? ? nil : r
  end

  def Instructor.add_unless_dup(fields)
    if count(:all, :conditions => {
      :department => fields[:department],
      :first_name => fields[:first_name],
      :last_name => fields[:last_name]
      }) > 0
      return find(:first, :conditions => fields)
    else
      return Instructor.create(fields)
    end
  end

end
